package in.muton.edushare.components.tasks.electives.api;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.BatchDAO;
import com.edushare.common.vo.Batches;
import com.edushare.common.vo.Curricula;
import com.edushare.common.vo.Users;

public class UserElectiveManager {

	public UserElectiveManager(IModel<Users> userModel) {
		this.userModel = userModel;
	}

	public List<ElectivePanelInitInfo> getUniquePanels() {
		List<ElectivePanelInitInfo> epiiList = new ArrayList<ElectivePanelInitInfo>();
		List<Batches> batchList = new BatchDAO(EduShareSession.get()
				.getSessionORMInterface())
				.getUserBatches(userModel.getObject());

		for (Batches b : batchList) {
			if(b.isGraduated())
				continue;
			List<Integer> uniqueGroupList;
			IModel<Curricula> cm = new CayenneModel<Curricula>(b.getCurricula());
			Electives elect = new Electives(userModel, cm, b
					.getCurrentSemester());
			uniqueGroupList = elect.getUniquePanels();
			for (Integer i : uniqueGroupList) {
				epiiList.add(new ElectivePanelInitInfo(cm, userModel, i, b
						.getCurrentSemester()));
			}
		}
		return epiiList;
	}

	public List<ElectivePanelInitInfo> getCommonPanels() {
		List<ElectivePanelInitInfo> epiiList = new ArrayList<ElectivePanelInitInfo>();
		List<Batches> batchList = new BatchDAO(EduShareSession.get()
				.getSessionORMInterface())
				.getUserBatches(userModel.getObject());

		for (Batches b : batchList) {
			if(b.isGraduated())
				continue;
			List<List<Integer>> commonGroupList;
			IModel<Curricula> cm = new CayenneModel<Curricula>(b.getCurricula());
			Electives elect = new Electives(userModel, cm, b
					.getCurrentSemester());
			commonGroupList = elect.getCommonElectiveGroupIds();
			for (List<Integer> i : commonGroupList) {
				epiiList.add(new ElectivePanelInitInfo(cm, userModel, i, b
						.getCurrentSemester()));
			}
		}
		return epiiList;
	}

	private IModel<Users> userModel;
}
